/************************************************************************************/
/*  Program name CAPFAIMP_ADDITIONS_B.sql                                        	*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 6 JAN, 2006   NIP Karen         First created                               		*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_ADDITIONS_B';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_ADDITIONS_B;
	
	lv_Next_Seq_ID		NUMBER;
	
	CURSOR Cur_Update IS
	SELECT
		FAB.*
	FROM
		FA_ADDITIONS_B				FAB
	,	CRST_FA_ADDITIONS_B_MAP		CFA
	WHERE
		FAB.Asset_ID = CFA.Asset_ID_N
	AND	FAB.Parent_Asset_ID	IS	NOT	NULL;

BEGIN
	UPDATE
		FA_ADDITIONS_B
	SET	
		ASSET_NUMBER	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(ASSET_NUMBER,1,4), ASSET_NUMBER),
		TAG_NUMBER	=	NULL
	WHERE ASSET_ID IN
		(
		SELECT 
			ASSET_ID
		FROM 
			CRST_FA_ADDITIONS_B
		);

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	FA_ADDITIONS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;
			
			INSERT INTO CRST_FA_ADDITIONS_B_MAP 
			VALUES
			(
			C1.ASSET_ID,   
			CAPBRIMP_MAPPING.RenameStr(SUBSTR(C1.ASSET_NUMBER,1,4),C1.ASSET_NUMBER),
			CAPBRIMP_MAPPING.RenameStr(SUBSTR(C1.TAG_NUMBER,1,4),C1.TAG_NUMBER),
			lv_Next_Seq_ID,     
			C1.ASSET_NUMBER,
			C1.TAG_NUMBER
			);

			INSERT INTO FA_ADDITIONS_B
			VALUES
			(
			lv_Next_Seq_ID,
			C1.ASSET_NUMBER,
			CAPBRIMP_MAPPING.GetAssetKeyMapping(C1.ASSET_KEY_CCID),
			C1.CURRENT_UNITS,
			C1.ASSET_TYPE,
			C1.TAG_NUMBER,
			C1.ASSET_CATEGORY_ID,
			C1.PARENT_ASSET_ID,
			C1.MANUFACTURER_NAME,
			C1.SERIAL_NUMBER,
			C1.MODEL_NUMBER,
			C1.PROPERTY_TYPE_CODE,
			C1.PROPERTY_1245_1250_CODE,
			C1.IN_USE_FLAG,
			C1.OWNED_LEASED,
			C1.NEW_USED,
			C1.UNIT_ADJUSTMENT_FLAG,
			C1.ADD_COST_JE_FLAG,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE16,
			C1.ATTRIBUTE17,
			C1.ATTRIBUTE18,
			C1.ATTRIBUTE19,
			C1.ATTRIBUTE20,
			C1.ATTRIBUTE21,
			C1.ATTRIBUTE22,
			C1.ATTRIBUTE23,
			C1.ATTRIBUTE24,
			C1.ATTRIBUTE25,
			C1.ATTRIBUTE26,
			C1.ATTRIBUTE27,
			C1.ATTRIBUTE28,
			C1.ATTRIBUTE29,
			C1.ATTRIBUTE30,
			C1.ATTRIBUTE_CATEGORY_CODE,
			C1.CONTEXT,
			C1.LEASE_ID,
			C1.INVENTORIAL,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.CREATION_DATE,
			C1.LAST_UPDATE_LOGIN,
			C1.GLOBAL_ATTRIBUTE1,
			C1.GLOBAL_ATTRIBUTE2,
			C1.GLOBAL_ATTRIBUTE3,
			C1.GLOBAL_ATTRIBUTE4,
			C1.GLOBAL_ATTRIBUTE5,
			C1.GLOBAL_ATTRIBUTE6,
			C1.GLOBAL_ATTRIBUTE7,
			C1.GLOBAL_ATTRIBUTE8,
			C1.GLOBAL_ATTRIBUTE9,
			C1.GLOBAL_ATTRIBUTE10,
			C1.GLOBAL_ATTRIBUTE11,
			C1.GLOBAL_ATTRIBUTE12,
			C1.GLOBAL_ATTRIBUTE13,
			C1.GLOBAL_ATTRIBUTE14,
			C1.GLOBAL_ATTRIBUTE15,
			C1.GLOBAL_ATTRIBUTE16,
			C1.GLOBAL_ATTRIBUTE17,
			C1.GLOBAL_ATTRIBUTE18,
			C1.GLOBAL_ATTRIBUTE19,
			C1.GLOBAL_ATTRIBUTE20,
			C1.GLOBAL_ATTRIBUTE_CATEGORY
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	FOR C2 IN Cur_Update LOOP
		BEGIN
			UPDATE
				FA_ADDITIONS_B
			SET
				Parent_Asset_ID	=	CAPBRIMP_MAPPING.GetAssetIDMAPPING(C2.Parent_Asset_ID)
			WHERE	Asset_ID	=	C2.Asset_ID;
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
	END LOOP;
		
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/